// packageA/pages/allot/allot.js
const app = getApp()

import customerApi from "../../../../api/index.js"
import hasPermission from "../../../../utils/auth"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: '',
    allotCur:0,
    allData:[],
    index:0,
    page:[],
    total: [],
    permissionType:1,
	  follow_status:"",
    group_id:'',
    group_type:0,
    customer_id:'',
    select_group_id:'',
    color:'#cd1e1f',
    timeArrow:false,
    showGroup:false,
    showContent:false,
    showShadow:false,
    showSuccess:false,
    isFixed:false,
    selectData:[],
    self_allot:false,
    allot_user: false,
    allot_group:false,
    repeat_allot:false,
    userIds:[],
    department:[],
    user:[],
    showData:[
      {
        label:'服装/手袋',
        value:1
      },
      {
        label:'机械、器械类',
        value:1
      },
      {
        label:'塑料、塑胶类',
        value:1
      },
      
      {
        label:'玻璃制品类',
        value:1
      },
      {
        label:'家具、家私类',
        value:1
      },
      {
        label:'重工业类',
        value:1
      },
      {
        label:'五金制品类',
        value:1
      },
      {
        label:'电子、电器类',
        value:1
      },
      {
        label:'木制品类',
        value:1
      }
    ]
  },
  tabSelect(e) {
    let index = e.currentTarget.dataset.id == '' ? 0 : parseInt(e.currentTarget.dataset.id) +1
    var currentTab = e.currentTarget.dataset.id
    if(!this.data.page[index]){
      this.data.page[index]=1
    }
    if(this.data.TabCur == currentTab){
      return
    }else{
      this.setData({
        TabCur: e.currentTarget.dataset.id,

        index:index,
        page: this.data.page
      })

      if(!this.data.allData[index]){
        this.loadData()
      }
    }
  },
  tabFollowSelect(e) {
    var currentTab = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.id == '' ? 0 : parseInt(e.currentTarget.dataset.id) + 1
    if (!this.data.page[index]) {
      this.data.page[index] = 1
    }
    console.log(currentTab)
    console.log('index',index)
    if(this.data.follow_status == currentTab){
      return
    }else{
      this.setData({
        follow_status: e.currentTarget.dataset.id,
        TabCur: currentTab,

        index: index,
        page: this.data.page
      })
      if (!this.data.allData[index]) {
        this.loadData()
      }
    }
  },
 
  selectBtn:function(e){
   // let selectData = this.data.selectData

 /*   if (selectData[e.currentTarget.dataset.index] === true) {
      console.log(11)
      selectData[e.currentTarget.dataset.index] = false
    } else {
      selectData = [];//改成单选卧槽    
      selectData[e.currentTarget.dataset.index] = true
    }*/
    if (this.data.group_type == 0 || this.data.group_type ==2) {
    if (e.currentTarget.dataset.index == this.data.selectData){
      this.setData({
        selectData:'',
        group_id:''
      })
    }else{
      this.setData({
        selectData: e.currentTarget.dataset.index,

      })
    }
    }
    if (this.data.group_type == 1) {
      if (e.currentTarget.dataset.index == this.data.selectData) {
        this.setData({
          selectData: '',
          select_group_id: ''
        })
      } else {
        this.setData({
          selectData: e.currentTarget.dataset.index,
         select_group_id: e.currentTarget.dataset.index
        })
      }

    }
  },
  allotTime(e){
    this.setData({
      allotCur: e.currentTarget.dataset.id,
      timeArrow: !this.data.timeArrow, 

    })
    wx.startPullDownRefresh()//下拉刷新
  },
  allotSelect(e) {
  
    let child = this.selectComponent('#tanchuang') 
    console.log(child)
        child.setData({
          allotCur: e.currentTarget.dataset.id,
          showShadow: true,
          group_type:0,
          showGroup: true,
          isFixed: true,
          color:"#cd1e1f",
          list:this.data.department
        })
  },
  /**
   * 
   * 获取user
   */
  getSelect: function (e) {
    console.log(e)
    customerApi.getSecondDepartUser({groupIds:e.detail}).then(
      res=>{
        console.log(res)
        console.log(res.data.data)

        let child = this.selectComponent('#Usertanchuang') 
        child.setData({
          selectData: [],
          showShadow: true,
          group_type: 0,
          showGroup: true,
          isFixed: true,
          color: "#cd1e1f",
          list: res.data
        })
      }
    )
  },
  /**
   * 选择user
   */
  getSelectUser:function(e){
    let userIds=e.detail
    this.setData({
      userIds:e.detail
    })

    wx.startPullDownRefresh()//下拉刷新
  },

  allotBtn:function(e){
    console.log(e)
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        showShadow: true,
        showGroup: true,
        isFixed: true,
        group_type: e.currentTarget.dataset.index,
        customer_id: e.currentTarget.dataset.id,
    
      })
      wx.showLoading({
        title: '加载中',
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
      customerApi.getDepartments({ customer_id: e.currentTarget.dataset.id}).then(res => {

        this.setData({
          showData: res.res
        })
        wx.hideLoading()
      })
    }
    if (e.currentTarget.dataset.index == 2) {
      this.setData({
        showShadow: true,
        showGroup: true,
        isFixed: true,
        group_type: e.currentTarget.dataset.index,
        customer_id: e.currentTarget.dataset.id,
      })
      customerApi.getUsers({customer_id:this.data.customer_id}).then(res => {
       

        this.setData({
          user: res.data,
    
          showData: res.data
        })
      })
  
    }
  },
  selfBtn:function(e){
   let customer_id= e.currentTarget.dataset.id
    wx.showModal({
      title: '确定跟进',
      content: '确定自己跟进',
      success:res=> {
        if (res.confirm) {
          customerApi.shareUserMe({customer_id:customer_id}).then(res=>{
            this.setData({
              showShadow: true,
              showSuccess: true,
              showGroup: false,
              isFixed: true,
              selectData: '',
              allData: [],
              page: 1
            })
            wx.hideLoading()
            wx.startPullDownRefresh()//下拉刷新
            return 
          })    
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }  
    })
  },
  submitBtn:function(e){

    //筛选结果
      if(this.data.group_type==0){
        this.setData({
          showGroup: false,
          allData:[],
          page: 1, 
          showShadow: false,
          isFixed: false,
          group_id: this.data.selectData
        })
        wx.startPullDownRefresh()//下拉刷新
        return
      }
    if (this.data.group_type == 2) {
      wx.showLoading({
        title: '请求数据中...',
      })
      customerApi.shareUserCustomer({ ower_user_id: this.data.selectData, customer_id: this.data.customer_id}).then(res=>{
        this.setData({
          showShadow: true,
          showSuccess: true,
          showGroup: false,
          isFixed: false,
          selectData:'',
        })
        console.log('event')
        this.eventUser(this.data.customer_id)

        wx.hideLoading()
        
        setTimeout(()=>{
          this.setData({
            showSuccess: false,
            showShadow: false,
          })
        },1500)
        

      })
 
      return
    }
    if (this.data.group_type == 1) {
      wx.showLoading({
        title: '请求数据中...',
      })
      customerApi.shareGroupCustomer({ customer_id: this.data.customer_id,group_id:this.data.select_group_id }).then(res=>{
        this.setData({
          showShadow: true,
          showSuccess: true,
          showGroup: false,
          isFixed: true,
          selectData: '',
        
        })
        wx.hideLoading()
        this.eventUser(this.data.customer_id)
        setTimeout(() => {
          this.setData({
            showSuccess: false,
            showShadow: false,
          })
        }, 1500)
  
      })
   
   
      return
    }
   
  },
  /**
   * 
   * 分配人员后触发
   */
  eventUser(customer_id){
    console.log(customer_id)
    customerApi.getCustomerDetails({
      customer_id: customer_id
    }).then(res => {
      console.log(this.data.allData[0])
      if(this.data.userInfo.auth_map==1){
        if (this.data.allData[0]) {
          let alldata = this.data.allData[0]
          let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          console.log(index)
          if (index != -1) {
            this.data.allData[0][index] = res.res
          }
        }
        console.log('data2', this.data.allData[2])
        if (this.data.allData[2]) {
          let alldata = this.data.allData[2]
          let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          console.log(index)
          if (index != -1) {
            this.data.allData[2][index] = res.res
            if (res.res.follow_status != 1) {
              this.data.allData[2].splice(index, 1)
            }
          }else{
            if (res.res.follow_status == 1) {
            this.data.allData[2].unshift(res.res)
            }
          }
        
        }
        if (this.data.allData[3]) {
          let alldata = this.data.allData[3]
          let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          if (index != -1) {
            this.data.allData[3][index] = res.res
            if (res.res.follow_status != 2) {
              this.data.allData[3].splice(index, 1)
            }
          } else {
            if (res.res.follow_status == 2) {
              this.data.allData[3].unshift(res.res)
            }
          
          }

        }
        if (this.data.allData[4]) {
          let alldata = this.data.allData[4]
          let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          if (index != -1) {
            this.data.allData[4][index] = res.res
            if (res.res.follow_status != 3) {
              this.data.allData[4].splice(index, 1)
            }
          } else {
            if (res.res.follow_status == 3) {
              this.data.allData[4].unshift(res.res)
            }
        
          }

        }
        
      }else{
      if (this.data.allData[0]) {
        let alldata = this.data.allData[0]
        let index = alldata.findIndex(item=>{
            return item.customer_id==customer_id
        })
        console.log(index)
        if(index!=-1){
          this.data.allData[0][index]=res.res
        }
      }
      if(this.data.allData[1]){
          let alldata = this.data.allData[1]
        let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          if (index != -1) {
            this.data.allData[1].splice(index, 1)
          }
          if(res.res.status==0){
            this.data.allData[1].unshift(res.res)
          }      
        
        }
      if (this.data.allData[2]) {
        let alldata = this.data.allData[2]
        let index = alldata.findIndex(item => {
          return item.customer_id == customer_id
        })
        if (index != -1) {
          this.data.allData[2].splice(index,1)
        }
        if (res.res.status == 1) {
            this.data.allData[2].unshift(res.res)
        }   
      }
      if (this.data.allData[3]) {
        let alldata = this.data.allData[3]
        if (res.res.status == 2) {
          let index = alldata.findIndex(item => {
            return item.customer_id == customer_id
          })
          if (index != -1) {
            this.data.allData[3][index]=res.res
          }else{
            this.data.allData[3].unshift = res.res
          }
      
        } 
      }
      }
      console.log(this.data.allData)
     this.setData({
          allData:this.data.allData
      })
    })
   },
  closeBtn:function(){
    this.setData({
      showShadow:false,
      showGroup:false,
      showSuccess:false,
      isFixed:false
    })
  },
  // 加载数据
  loadData:function(){
    wx.showLoading({
      title: '加载中...',
    })
    let index = this.data.TabCur == '' ? 0 : parseInt(this.data.TabCur)+1
    console.log(index)
    console.log(this.data.page)
    if (!this.data.page[index]){
      this.data.page[index]=1
    }
    let tabcur = this.data.TabCur
    if (this.data.userInfo.auth_map==1){
      tabcur=''
    }
    customerApi.getCustomers({
      page:this.data.page[index],
      status: tabcur,
      timeArrow:this.data.timeArrow,
      userIds:this.data.userIds,
      group_id:this.data.group_id,
	    follow_status:this.data.follow_status
    }).then(res=>{
      console.log('res',res)
      if(!this.data.allData[index]){
          this.data.allData[index]=[]
      }
      console.log('allData',this.data.allData)
      console.log('allData1',    this.data.allData[index].concat(res.data))
      this.data.allData[index] = this.data.allData[index].concat(res.data)
      this.data.total[index] = res.meta.total
      this.setData({
        allData:this.data.allData,
        total: this.data.total,
        index:index
      })
      wx.stopPullDownRefresh()
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    app.CheckSession().then(res => {
      if (res == true) {
        app.GetUser().then(data => {
          this.setData({
            userInfo: data,
            allData: [],
            page: [],
            allot_group: hasPermission('allot_group'),
            allot_user: hasPermission('allot_user'),
            self_allot: hasPermission('self_allot'),
            repeat_allot: hasPermission('repeat_allot'),

          })
          
          customerApi.getDepartments().then(res=>{
              this.setData({
                department:res.res
              })
          })
      
        this.loadData()

        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '加载中...',
    })
    let index = this.data.TabCur != '' ? parseInt(this.data.TabCur)+1 : 0; 
    console.log(index)
    this.data.allData[index]=[]
    this.data.page[index]=1
    this.setData({
      allData:this.data.allData,
      page:this.data.page,
      index:index
    })
    this.loadData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let index =this.data.index
    if(this.data.allData[index].length == this.data.total[index]){
      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    this.data.page[this.data.index] = this.data.page[this.data.index]+1
    this.setData({
      page:this.data.page 
    })
    this.loadData()
  },


})